﻿using Microsoft.EntityFrameworkCore;
using Sgr.Data;
using Sgr.EntityFrameworkCore;
using Sgr.UPMS.Domain.Departments;
using Sgr.UPMS.Domain.Duties;
using Sgr.UPMS.Domain.LogLogins;
using Sgr.UPMS.Domain.Menus;
using Sgr.UPMS.Domain.Organizations;
using Sgr.UPMS.Domain.Roles;
using Sgr.UPMS.Domain.Tokens;
using Sgr.UPMS.Domain.Users;
using Sgr.UPMS.Domain.UserTokens;
using Sgr.UPMS.Infrastructure.EntityConfigurations;

namespace Sgr.UPMS.Infrastructure
{
    /// <summary>
    /// UPMS模块的数据库上下文
    /// </summary>
    public class UpmsDbContext : UnitOfWorkDbContext
    {
        /// <summary>
        /// 初始化UPMS模块的数据库上下文
        /// </summary>
        /// <param name="options">数据库上下文选项</param>
        public UpmsDbContext(DbContextOptions<UpmsDbContext> options) : base(options)
        {
        }

        #region 组织架构相关

        public DbSet<Organization> Organizations { get; set; }
        public DbSet<OrganizationInvitation> OrganizationInvitations { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Duty> Duties { get; set; }

        #endregion 组织架构相关

        #region 用户角色相关

        public DbSet<Role> Roles { get; set; }
        public DbSet<RoleResource> RoleResources { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<UserDuty> UserDuties { get; set; }
        public DbSet<UserRole> UserRoles { get; set; }

        #endregion 用户角色相关

        #region 认证授权相关

        public DbSet<UserRefreshToken> UserRefreshTokens { get; set; }
        public DbSet<TemporaryToken> TemporaryTokens { get; set; }

        #endregion 认证授权相关

        #region 系统功能相关

        public DbSet<Menu> Menus { get; set; }
        public DbSet<LogLogin> LogLogins { get; set; }

        #endregion 系统功能相关

        #region 集成事件

        //public DbSet<OutboxMessage> OutboxMessages { get; set; }

        #endregion 集成事件

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //// 自动扫描当前程序集中的所有 IEntityTypeConfiguration 配置类
            //modelBuilder.ApplyConfigurationsFromAssembly(typeof(UpmsDbContext).Assembly);

            DatabaseType? databaseType = this.GetDatabaseType();

            #region 组织架构相关配置

            modelBuilder.ApplyConfiguration(new OrganizationEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new OrganizationInvitationEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new DepartmentEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new DutyEntityTypeConfiguration(databaseType));

            #endregion 组织架构相关配置

            #region 用户角色相关配置

            modelBuilder.ApplyConfiguration(new RoleEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new RoleResourceEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new UserEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new UserDutyEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new UserRoleEntityTypeConfiguration(databaseType));

            #endregion 用户角色相关配置

            #region 认证授权相关配置

            modelBuilder.ApplyConfiguration(new UserRefreshTokenConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new TemporaryTokenEntityTypeConfiguration(databaseType));

            #endregion 认证授权相关配置

            #region 系统功能相关配置

            modelBuilder.ApplyConfiguration(new MenuEntityTypeConfiguration(databaseType));
            modelBuilder.ApplyConfiguration(new LogLoginEntityTypeConfiguration(databaseType));

            #endregion 系统功能相关配置

            #region 集成事件

            //modelBuilder.ApplyConfiguration(new OutboxMessageConfiguration(databaseType));

            #endregion 集成事件

            // 为所有实体的 DateTime / DateTime? 属性应用 UTC 值转换器
            modelBuilder.ApplyUtcDateTimeConverters();
        }
    }
}